﻿using System.Security.Cryptography;
using System.Text;

namespace ArienToolkits.Cryptography
{
    /// <summary>
    /// MD5加密类
    /// </summary>
    public static class MD5
    {
        /// <summary>
        /// 加密处理
        /// </summary>
        /// <param name="srcText"></param>
        /// <returns></returns>
        public static string Encrypto(string srcText)
        {
            if (string.IsNullOrEmpty(srcText))
            {
                return string.Empty;
            }
            System.Security.Cryptography.MD5 md5 = new MD5CryptoServiceProvider();
            byte[] md5Data = md5.ComputeHash(System.Text.Encoding.Default.GetBytes(srcText));
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < md5Data.Length; i++)
            {
                builder.Append(md5Data[i].ToString("X2"));
            }

            return builder.ToString();
        }

        #region 扩展方法
        /// <summary>
        /// 转换为MD5加密字符串
        /// </summary>
        /// <param name="srcText"></param>
        /// <returns></returns>
        public static string ToMD5(this string srcText)
        {
            return Encrypto(srcText);
        }
        #endregion
    }
}
